<?php

namespace Main\Model;

// 控制器基类
use FC\Model\BaseModel;

class articles extends BaseModel
{
	
    function __construct()
    {
		// 数据库配置类型,默认为Mysql
		//$this->db_type = 'Mysql';		
		// 数据库配置链接名称,默认为default
		//$this->db_config_name = 'default';
		
		// 执行父类
        parent::__construct();		
    }
	
    // 查询年份
    public function nian(){
        $sql = 'select nian from '.$this->table.' group by nian';
        $re = $this->db->sql($sql)->fetchall();
        return $re;
    }	
	
    /**
     * 获取数据(重写这个函数,让他支持联表查询)
     *
     * @return array
     */
    public function query(array $datas = [])
    {
        $page   = (int)  isset($datas['page']) ? $datas['page'] : 1;
        $limit  = (int) isset($datas['limit']) ? $datas['limit'] : 10;
        $offset = (int) isset($datas['offset']) ? $datas['offset'] : 0;
        $skey   = isset($datas['skey']) ? $datas['skey'] : '';
        $sname  = isset($datas['sname']) ? $datas['sname'] : '';
        unset($datas['sname']);
        unset($datas['skey']);
        unset($datas['page']);
        // 表名
        $table = $this->table;
        // 检测变量值
        if (!empty($skey) && !empty($sname)) {
            // 搜索这个值
            $datas[$skey] = ['LOCATE', $sname];
        }
        $where  = $this->filterValue($datas);
        // 排序方式
        $order  = (isset($datas['order']) && $datas['order'] === 'desc') ? 'desc'  : 'asc';
        // 排序字段 联合查询这里要个别名
        $sortby = isset($datas['sortby']) ? $datas['sortby'] : 'a.'.$this->primary;
        // 获取数量
        $number   = $this->db->name($table,'a')->join('views','a.aid = v.aid','left','v')->where($where)->number();
		//echo $this->db->lastsql();
        // 获取分页值
        list($total, $offset2) = $this->page($number, $page, $limit);
        // 看看从哪里开始
        if ($offset != 0) {
            $offset2 = $offset;
        }
        $limit = $offset2 . ',' . $limit;
        $res   = $this->db->getid('a.*,v.content')->name($table,'a')->join('views','a.aid = v.aid','left','v')->where($where)->order($sortby, $order)->limit($limit)->fetchall();
        //echo $this->db->lastsql();
        if ($res) {
            return ['data' => $res, 'page' => ['page' => $page, 'number' => $number, 'total' => $total, 'offset' => $offset]];
        } else {
            return null;
        }
    }	
	
    /**
     * 获取数据
     *
     * @return array
     */
    public function get(int $id)
    {
		if(empty($id)){
			return false;
		}
        // 表名
        $table = $this->table;
		$where['a.aid'] = $id; 
        $res   = $this->db->name($table,'a')->join('views','a.aid = v.aid','left','v')->where($where)->limit(1)->fetch();
        if ($res) {
            return $res;
        } else {
            return null;
        }
    }	
	
    /**
     * 获取关键字和描述
     *
     * @return array
     */
    public function getKeyword(int $id)
    {
		if(empty($id)){
			return false;
		}
        // 表名
        $table = $this->table;
		$where['aid'] = $id; 
        $res   = $this->db->name($table)->getid('title,keyword,description')->where($where)->limit(1)->fetch();
        if ($res) {
            return $res;
        } else {
            return null;
        }
    }	

    // 更新阅读数量	
	public function update_readnum($id){
		if(!$id){
			return false;
		}
        // 表名
        $table = $this->table;		
		$where['aid'] = $id;
        $data = array('readnum' => 'readnum+1');
        $this->db->name($table)->where($where)->upd($data);		
		return true;
	}
	
    // 随机获取一条数据 
    public function rand_views()
    {
        $sql = 'SELECT * FROM fc_articles WHERE status=1 and aid >= ((SELECT MAX(aid) FROM fc_articles)-(SELECT MIN(aid) FROM fc_articles)) * RAND() + (SELECT MIN(aid) FROM fc_articles) LIMIT 1';
        $re = $this->db->sql($sql)->fetch();
        return $re;
    }
	
}